//
//  RootViewController.m
//  DrillDownExample
//

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

	self.title = @"Countries";
	arrCountries = [[NSArray alloc] initWithObjects:@"Australia", @"United States", nil];
	arrUSACities = [[NSArray alloc] initWithObjects:@"Boston", @"San Francisco", @"New York", nil];
	arrAustraliaCities = [[NSArray alloc] initWithObjects:@"Brisbane", @"Perth", @"Sydney", nil];
	
	details = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
    if (indexPath.row == 0)
		[details setContent:arrAustraliaCities];
	else
		[details setContent:arrUSACities];
	
	[[self navigationController] pushViewController:details animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arrCountries count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [arrCountries objectAtIndex:indexPath.row];
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    return cell;
}

- (void)dealloc {
	[arrCountries release];
	[arrUSACities release];
	[arrAustraliaCities release];
	[details release];
	
    [super dealloc];
}

@end
